package csu.rjxyg.action.userAction;

import csu.rjxyg.action.BaseAction;
import csu.rjxyg.bean.core.BaseBean;
import csu.rjxyg.bean.core.BaseObject;
import csu.rjxyg.action.constant.UserConstant;
import csu.rjxyg.factory.core.UtilFactory;
import csu.rjxyg.service.BaseUserService;
import csu.rjxyg.util.LogUtil;

public class InsertUserAction extends BaseAction{
	private LogUtil log;
	
	
	private String userName;
	private String userId;
	private String userPass;
	private String[] userPermission;
	
	
	public InsertUserAction(){
		log = UtilFactory.getLogUtil(getClass());
	}
	
	public void insertUseBean() {
		String name = null;
		try {
			BaseUserService userService = (BaseUserService) factory.getBean(BaseAction.USER_SERVICE);
			BaseObject object = (BaseObject) this.getSession().get(UserConstant.USER_SESSION);
			name = (String) object.get(UserConstant.USER_ID);

			userService = (BaseUserService) factory.getBean(BaseAction.USER_SERVICE);
			
			int[] permissions = this.getPermissionUpAndDown(Integer.parseInt((String) object.get(UserConstant.USER_LOGIN_QX)));
			
			for( int i = 0;i <  userPermission.length;i++){
				int j = Integer.parseInt(userPermission[i]);
				
				if ( j < permissions[0]  || j > permissions[1]){
					log.info(name + "新建用户信息失败");
					return;
				}
			}
			
			BaseObject temp = (BaseObject) factory
					.getBean(BaseBean.BEAN_OBJECT);

			temp.set(UserConstant.USER_ID, userId);
			temp.set(UserConstant.USER_PASS, userPass);
			temp.set(UserConstant.USER_NAME, userName);
			temp.set(UserConstant.USER_QX, userPermission);
			temp.set(UserConstant.USER_LOGIN_QX, userPermission[0]);

			if (userService.insertUserBean(temp)) {
				log.info(name + "新建用户信息成功，信息如下: " + temp.getBean());
			}else{
				log.info(name + "新建用户信息失败");
			}
		} catch (Exception e) {
			log.info(name + "新建用户信息失败");
			e.printStackTrace();
			log.info(e.getMessage());
		}

	}
	
	
	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public String getUserPass() {
		return userPass;
	}

	public void setUserPass(String userPass) {
		this.userPass = userPass;
	}

	public String[] getUserPermission() {
		return userPermission;
	}

	public void setUserPermission(String[] userPermission) {
		this.userPermission = userPermission;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}
}
